<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

class products extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    //爬取接口数据(GET)
    public function paqu_jiekou($url,$data = null,$type = 1)
    {
        //初始化cURL会话
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($data)){
            //携带请求参数
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        if ($type == "pdd"){
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/x-www-form-urlencoded;charset=utf-8'
            ));
        }else{
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Accept: application/json',
            ));
        }
        //使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        //停止cURL验证对端证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //1.检查SSL对端证书中是否存在通用名称。(已弃用)
        //2.检查公共名称的存在，并验证它是否与所提供的主机名匹配。0表示不检查名称。在生产环境中，该选项的值应该保持为2(默认值)。
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //执行cURL会话
        $response = curl_exec($ch);
        // 检查是否有错误发生
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        //关闭cURL会话
        curl_close($ch);
        $data = json_decode($response, true);
        //历史信息
        return $data;
    }

    public function paqu_jiekou_POST($url,$data = null,$type = 1)
    {
        // 初始化cURL会话
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        // 设置请求参数
        if (!empty($data)) {
            // 对数据进行JSON编码
            $json_data = json_encode($data);
            // 设置POST请求的数据
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
            // 设置请求头为JSON格式
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Accept: application/json',
            ));
            // 使用POST方法
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        } else {
            // 使用GET方法
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        }

        // 停止cURL验证对端证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 1.检查SSL对端证书中是否存在通用名称。(已弃用)
        // 2.检查公共名称的存在，并验证它是否与所提供的主机名匹配。0表示不检查名称。在生产环境中，该选项的值应该保持为2(默认值)。
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        // 将curl_exec()获取的信息以文件流的形式返回，而不是直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 执行cURL会话
        $response = curl_exec($ch);

        // 检查是否有错误发生
        if (curl_errno($ch)) {
            return curl_error($ch);
        }

        // 关闭cURL会话
        curl_close($ch);

        // 解析JSON格式的响应数据
        $data = json_decode($response, true);

        // 返回解析后的数据
        return $data;
    }

    /**
     * 拼多多
     * 采集的两种方式
     * 方式一：通过循环的方式获取全部数据（数据太多时候，容易超时）
     * 方式二：通过跳转的方式全部采集（缺点：速度太慢）
     */

    //方式一：通过循环的方式获取全部数据（数据太多时候，容易超时）
    //商品列表
    public function pdd_goods_list()
    {
        try {
            $apikey = CC('mayi_apikey');
            $page = $this->request->param('page',1);
            $page_size = $this->request->param('page_size',100);
            $type = $this->request->param('type',0);
            $goods_id = $this->request->param('gooods_id');
            switch ($type){
                case 0:
                    $url = "http://api-gw.haojingke.com/index.php/v1/api/pdd/goodslist?apikey={$apikey}&page={$page}&page_size={$page_size}&keyword=$goods_id";
                    $data = $this->paqu_jiekou($url);
                    $new_data = $data['data']['goods_list'];
                    break;
                case 1:
                    $data = $this->get_goods_list($apikey,5,1,100,$goods_id);
                    $new_data = $data['goods_list'];
                    $new_page = floor($data['total'] / 100) - 4;
                    $data2 = $this->get_goods_list($apikey,6,$new_page,100,$goods_id);
                    $new_data = array_merge($new_data, $data2['goods_list']);
                    break;
            }

            $pdd_goods_list = Db::name('pddss')->field("goods_id")->select();
            $insert_goods = [];
            $data_goods = [];
            foreach ($new_data as $item){
                $data_goods['goods_id'] = $item['goods_id'];
                $data_goods['image_url'] = $item['picurl'];
                $data_goods['title'] = $item['goods_name'];
                $data_goods['sales_num'] = $item['sales'];
                $data_goods['price'] = $item['price'];
                $data_goods['coupons_price'] = $item['price_after'];
                $data_goods['evaluation'] = $item['comments'];
                //24小时销量
                $data_goods['sales24_num'] = 0;
                //运费
                $data_goods['freight'] = 0;
                //好评率
                $data_goods['positive_review_rate'] = $item['desc_pct'];
                //上架时间
//            $data_goods['listing_time'] = $item['parent_cat_id'];
                //店铺名称
                $data_goods['shop_name'] = $item['shopname'];
                //店铺商品数
//                $data_goods['shop_goods_num'] = $item['shopid'];
                //店铺评分
//                $data_goods['shop_rating'] = $item['shopid'];
                //分类
                $cate_info = unserialize($item['pdd_c_info']);
                $data_goods['cate1_id'] = $cate_info[0];
                $data_goods['cate2_id'] = $cate_info[1];
                $data_goods['cate3_id'] = $cate_info[2];
                if (count($cate_info) > 3){
                    $data_goods['cate4_id'] = $cate_info[3];
                }else{
                    $data_goods['cate4_id'] = null;
                }
                //分类名称
                $data_goods['cate_name'] = $item['pf_cname'];
                //原链接
                $data_goods['detail_url'] = $item['materiaurl'];
                //判断是否已经插入过数据库
                foreach ($pdd_goods_list as $goods_item){
                    if ($goods_item['goods_id'] == $item['goods_id']){
                        unset($data_goods);
                    }
                }
                if (isset($data_goods)){
                    $insert_goods[] = $data_goods;
                }
            }
             Db::name('pddss')->insertAll($insert_goods);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('successful');

    }

    //递归循环获取所有列表
    private function get_goods_list($apikey,$num = 5,$page = 1, $pagesize = 100,$goods_id = 0){
        $url = "http://api-gw.haojingke.com/index.php/v1/api/pdd/goodslist?apikey={$apikey}&page={$page}&page_size={$pagesize}";
        if (!empty($goods_id)){
            $url = $url."&keyword=$goods_id";
        }

        $data = $this->paqu_jiekou($url);

        $datas['goods_list'] = !empty($data['data']['goods_list']) ? array_merge($datas['goods_list'] ?? [], $data['data']['goods_list']) : [];
        $datas['total'] = $data['data']['total'];

        if ($data['data']['total'] > bcmul($page, $pagesize) && $num > 1){
            $new_page = $page + 1;
            $new_num = $num - 1;
            $result = $this->get_goods_list($apikey, $new_num, $new_page, $pagesize,$goods_id);
            $datas['goods_list'] = array_merge($datas['goods_list'], $result['goods_list']);
        }
        return $datas;
    }


    //方式二：通过跳转的方式全部采集（缺点：速度太慢）
    //全部采集
    public function pdd_caiji()
    {
        $apikey = CC('mayi_apikey');
        $page = $this->request->param('page',1);
        $pagesize = $this->request->param('pagesize',100);
        $goods_id = $this->request->param('gooods_id');
        if (empty($cat_ids)){
            $data_goods_cate = $this->pdd_goods2_cateid(4);
            $cat_ids = $data_goods_cate['cat_id'];
        }
        $url = "http://api-gw.haojingke.com/index.php/v1/api/pdd/goodslist?apikey={$apikey}&page={$page}&page_size={$pagesize}&keyword=$goods_id";
        // 处理采集逻辑
        $data = $this->paqu_jiekou($url);

        if (!empty($data['data']['goods_list'])){
            $new_data = $data['data']['goods_list'];
            $pdd_goods_list = Db::name('pddss')
                ->field("goods_id")
                ->select();
            $insert_goods = [];
            $data_goods = [];
            foreach ($new_data as $item){
                $data_goods['goods_id'] = $item['goods_id'];
                $data_goods['image_url'] = $item['picurl'];
                $data_goods['title'] = $item['goods_name'];
                $data_goods['sales_num'] = $item['sales'];
                $data_goods['price'] = $item['price'];
                $data_goods['coupons_price'] = $item['price_after'];
                $data_goods['evaluation'] = $item['comments'];
                //24小时销量
                $data_goods['sales24_num'] = 0;
                //运费
                $data_goods['freight'] = 0;
                //好评率
                $data_goods['positive_review_rate'] = $item['desc_pct'];
                //上架时间
//            $data_goods['listing_time'] = $item['parent_cat_id'];
                //店铺名称
                $data_goods['shop_name'] = $item['shopname'];
                //店铺商品数
//                $data_goods['shop_goods_num'] = $item['shopid'];
                //店铺评分
//                $data_goods['shop_rating'] = $item['shopid'];
                //分类
                $cate_info = unserialize($item['pdd_c_info']);
                $data_goods['cate1_id'] = $cate_info[0];
                $data_goods['cate2_id'] = $cate_info[1];
                $data_goods['cate3_id'] = $cate_info[2];
                if (count($cate_info) > 3){
                    $data_goods['cate4_id'] = $cate_info[3];
                }else{
                    $data_goods['cate4_id'] = null;
                }
                //分类名称
                $data_goods['cate_name'] = $item['pf_cname'];
                //原链接
                $data_goods['detail_url'] = $item['materiaurl'];
                foreach ($pdd_goods_list as $pdd_goods_item){
                    if ($pdd_goods_item['goods_id'] == $item['goods_id']){
                        unset($data_goods);
                    }
                }
                if (isset($data_goods)){
                    $insert_goods[] = $data_goods;
                }

            }
            if (!empty($insert_goods)){
                Db::name('pddss')->insertAll($insert_goods);
            }
        }
        if (!empty($goods_id)){
            $this->success('successful',$data['data']['goods_list'][0]);
        }
        $newpage = $page+1;
        echo "分类为{$cat_ids}的第{$page}页采集成功，正在跳转至下第{$newpage}页....";
        sleep(1);

        //下一个三级分类的开始
        if (!empty($data['data']['goods_list']) && count($data['data']['goods_list']) == $pagesize){
            $new_cat_ids = $cat_ids;
        }
        else{
            //修改这个分类状态
            Db::name('pdd_cate')->where('cat_id',$cat_ids)->update(['goods_status' => 1]);

            $data_goods_status = $this->pdd_goods2_cateid();
            if (empty($data_goods_status)){
                Db::name('pdd_cate')->where('leaf',[3,4])->update(['goods_status' => 0]);
                exit('采集完成');
            }
            $new_cat_ids = $data_goods_status['cat_id'];
            $newpage = 1;
        }

        echo "<script>window.location.href='/api/products/pdd_caiji?cat_ids={$new_cat_ids}&page={$newpage}';</script>";
        exit();
    }

    //获得下一个三,四级分类
    public function pdd_goods2_cateid($level = 4)
    {
        $dd_cate = Db::name('pdd_cate')
            ->field('id,cat_id,cat_name,leaf,parent_cat_id,is_status,goods_status')
            ->where('leaf',$level)
            ->where('goods_status',0)
            ->find();
        return $dd_cate;
    }


    //方式一
    //分类列表
    public function pdd_goods_cate_list()
    {
        $apikey = CC('mayi_apikey');
        $parent_cat_id = $this->request->param('parent_cat_id',0);
        $level = intval($this->request->param('level', 1));
        if ($level == 1){
            $data = $this->other_cate_list($apikey,$parent_cat_id);
        }else{
            $leaf = $level - 1;
            $data = $this->get_cate_list($apikey,$leaf);
        }
        $pdd_cate_list = Db::name('pdd_cate')
            ->field('id,cat_id,cat_name,leaf,parent_cat_id,is_status')
            ->where('leaf',$level)
            ->select();
        $insert_cate = [];
        $data_cate = [];
        $new_cate_list = $data['data']['goods_cats_list'];
        foreach ($new_cate_list as $item){
            $data_cate['cat_id'] = $item['cat_id'];
            $data_cate['cat_name'] = $item['cat_name'];
            $data_cate['leaf'] = $item['level'];
            $data_cate['parent_cat_id'] = $item['parent_cat_id'];
            //判断是否已经插入过数据库
            foreach ($pdd_cate_list as $cate_item){
                if ($cate_item['cat_id'] == $item['cat_id']){
                    unset($data_cate);
                }
            }
            if (isset($data_cate)){
                $insert_cate[] = $data_cate;
            }

        }
        Db::name('pdd_cate')->insertAll($insert_cate);
        $this->success('successful',$data);
    }

    public function get_cate_list($apikey,$leaf = 1){
        try {
            set_time_limit(0);
            $cate_list = Db::name('pdd_cate')
                ->field('id,cat_id,cat_name,leaf,parent_cat_id,is_status')
                ->where('leaf',$leaf)
                ->where('is_status','<>',1)
                ->limit(30)
                ->select();
            if (empty($cate_list)){
                $this->error("该等级分类已经查完");
            }
            $cate_id_list = [];
            foreach ($cate_list as $cate_item){
                if (!in_array($cate_item['cat_id'],$cate_id_list)){
                    $cate_id_list[] = $cate_item['cat_id'];
                }
            }
            $data = [];
            $i = 1;
            foreach ($cate_id_list as $cate_id_item){
                $result = $this->other_cate_list($apikey,$cate_id_item);
                $data['data']['goods_cats_list'] = array_merge($data['data']['goods_cats_list'] ?? [], $result['data']['goods_cats_list']);
                $i++;
            }
            Db::name('pdd_cate')->where('cat_id','in',$cate_id_list)->update(['is_status' => 1]);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        return $data;
    }

    //查询分类列表
    private function other_cate_list($apikey,$parent_cat_id)
    {
        $url = "http://api-gw.haojingke.com/index.php/v1/api/pdd/cats?apikey={$apikey}&parent_cat_id={$parent_cat_id}";
        $data = $this->paqu_jiekou($url);
        return $data;
    }

    //方式二
    public function pdd_cate_caji()
    {
        $apikey = CC('mayi_apikey');
        $leaf = $this->request->param('leaf',2);
        $parent_cat_id = $this->pdd_cate($leaf);
        if (empty($parent_cat_id)){
            $leaf = $leaf +1;
            $parent_cat_new = array();
            for ($i = $leaf;$i<4;$i++){
                $parent_cat_new = $this->pdd_cate($leaf);
                if (!empty($parent_cat_new)){
                    $leaf = $i;
                    break;
                }
            }
            if (empty($parent_cat_new)){
                echo "采集完成";
                exit();
            }else{
                $parent_cat_id = $parent_cat_new['cat_id'];
            }
        }
        if ($leaf == 4){
            echo "采集完成";
            exit();
        }
        $url = "http://api-gw.haojingke.com/index.php/v1/api/pdd/cats?apikey={$apikey}&parent_cat_id={$parent_cat_id}";
        $data = $this->paqu_jiekou($url);

        $pdd_cate_list = Db::name('pdd_cate')
            ->field('id,cat_id,cat_name,leaf,parent_cat_id,is_status')
            ->select();
        $insert_cate = [];
        $data_cate = [];
        $new_cate_list = $data['data']['goods_cats_list'];
        foreach ($new_cate_list as $item){
            $data_cate['cat_id'] = $item['cat_id'];
            $data_cate['cat_name'] = $item['cat_name'];
            $data_cate['leaf'] = $item['level'];
            $data_cate['parent_cat_id'] = $item['parent_cat_id'];
            //判断是否已经插入过数据库
            if (!empty($pdd_cate_list)){
                foreach ($pdd_cate_list as $cate_item){
                    if ($cate_item['cat_id'] == $item['cat_id']){
                        unset($data_cate);
                    }
                }
            }
            if (isset($data_cate)){
                $insert_cate[] = $data_cate;
            }

        }
        Db::name('pdd_cate')->insertAll($insert_cate);

        Db::name('pdd_cate')->where('cat_id',$parent_cat_id)->update(['is_status' => 1]);

        echo "上级id为{$parent_cat_id}采集成功，正在跳转至下第一条....";

        sleep(1);

        echo "<script>window.location.href='/api/products/pdd_cate_caji?leaf={$leaf}';</script>";
        exit();
    }

    private function pdd_cate($leaf = 2)
    {
        $pdd_list = Db::name('pdd_cate')
            ->field('id,cat_id,cat_name,leaf,parent_cat_id,is_status')
            ->where('leaf',$leaf)
            ->where('is_status',0)
            ->find();
        return $pdd_list;
    }


    /**
     * 抖店
     */

    //采集商品(前500条)
    //方式一
    public function dd_gooods_list()
    {
        set_time_limit(0);
        try {
            $pub_id = CC('jtklm_pubId');
            $page = $this->request->param('page',1);
            $pageSize = $this->request->param('pageSize',20);
            $type = $this->request->param('type',0);
            $product_url = $this->request->param('product_url');
            //判断有无商品Id
            if (!empty($product_url)){
                //获得转链口令
                $url = "http://api.act.jutuike.com/dyfx/product_privilege?pub_id={$pub_id}&product_url={$product_url}&sid=123456";
                $linshi_data = $this->paqu_jiekou($url);
                if (empty($linshi_data['data'])){
                    $this->error('未找到该商品');
                }
                $common = $linshi_data['data']['dy_password'];
                $qinqiu = [
                    'pub_id' => $pub_id,
                    'command' => $common,
                ];
                $new_url = "http://api.act.jutuike.com/dyfx/shareCommandParse";
                $data_info = $this->paqu_jiekou_POST($new_url,$qinqiu);
                $new_data[] = $data_info['data']['product_info'];
            }else{
                switch ($type){
                    case 0:
                        $url = "http://api.act.jutuike.com/dyfx/product_search?pub_id={$pub_id}&page={$page}&pageSize={$pageSize}";
                        $data = $this->paqu_jiekou($url);
                        $new_data = $data['data'];
                        break;
                    case 1:
                        $data = $this->get_dd_goods($pub_id,1,20);
                        $new_data = $data['data'];
                        break;
                }
            }
            $products_list = [];
            $productsId_list = [];
            foreach ($new_data as $items){
                if (!in_array($items['product_id'],$products_list)){
                    $products_list[] = $items;
                    $productsId_list[] = $items['product_id'];
                }
            }

            //去重
            $products_list = array_unique($products_list,SORT_REGULAR);
            $dd_goods_list = Db::name('ddss')
                ->field('goods_id')
                ->where("goods_id",'in',$productsId_list)
                ->select();
            $dd_goodsID_list = [];
            //判断是否已经插入过数据库
            if (!empty($dd_goods_list)){
                foreach ($dd_goods_list as $dd_goods_item){
                    $dd_goodsID_list[] = $dd_goods_item['goods_id'];
                }
            }
            $insert_goods = [];
            $data_goods = [];
            foreach ($products_list as $item){
                if (!in_array($item['product_id'],$dd_goodsID_list)){
                    $data_goods['goods_id'] = $item['product_id'];
                    $data_goods['image_url'] = $item['cover'];
                    $data_goods['title'] = $item['title'];
                    if (strpos($item['sales'],"万") !== false){
                        $sales = explode("万",$item['sales']);
                        $data_goods['sales_num'] = bcmul($sales[0],10000);
                    }else{
                        $data_goods['sales_num'] = $item['sales'];
                    }
                    $data_goods['price'] = $item['price'];
                    $data_goods['coupons_price'] = $item['coupon_price'];
//                $data_goods['evaluation'] = $item['comments'];
                    //24小时销量
                    $data_goods['sales24_num'] = 0;
                    //运费
                    $data_goods['freight'] = 0;
                    //好评率
                    $data_goods['positive_review_rate'] = 0;
                    //上架时间
//            $data_goods['listing_time'] = $item['parent_cat_id'];
                    //店铺名称
                    $data_goods['shop_name'] = $item['shop_name'];
                    //店铺商品数
//                $data_goods['shop_goods_num'] = $item['shopid'];
                    //店铺评分
//                $data_goods['shop_rating'] = $item['shopid'];
                    //分类
                    $data_goods['cate1_id'] = $item['first_cid'];
                    $data_goods['cate2_id'] = $item['second_cid'];
                    $data_goods['cate3_id'] = $item['third_cid'];
                    //商品链接
                    $data_goods['detail_url'] = $item['detail_url'];
                    if (!empty($data_goods)){
                        $insert_goods[] = $data_goods;
                    }

                }
            }

            Db::name('ddss')->insertAll($insert_goods);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('successful',$products_list);
    }

    private function get_dd_goods($pub_id,$page = 1, $pageSize = 20,$num = 25)
    {
        //升序的历史销量
        $url = "http://api.act.jutuike.com/dyfx/product_search?search_type=1&sort_type=0&pub_id={$pub_id}&page={$page}&pageSize={$pageSize}";
        $data = $this->paqu_jiekou($url);

        $datas['data'] = array_merge($datas['data'] ?? [], $data['data']);

        if ($num > 1){
            $new_page = $page + 1;
            $num = $num - 1;
            $result = $this->get_dd_goods($pub_id, $new_page, $pageSize,$num);
            $datas['data'] = array_merge($datas['data'], $result['data']);
        }
        return $datas;
    }

    //根据Id查询商品
    public function get_dd_good_info($pub_id,$page = 1, $pageSize = 20,$keyword = 0,$num = 0)
    {
        $url = "http://api.act.jutuike.com/union/search?pub_id={$pub_id}&keyword={$keyword}&sid=123456&source=douyin&page={$page}&pageSize={$pageSize}";
        $data = $this->paqu_jiekou($url);
        foreach ($data['data'] as $datum){
            if ($datum['goodsId'] == $keyword){
                return $datum;
            }
        }
        if ($num > 1){
            $new_page = $page + 1;
            $num = $num - 1;
            $this->get_dd_good_info($pub_id,$new_page,$pageSize,$keyword,$num);
        }
    }

    //采集商品（跳转这个链接）
    //方式二
    public function dd_goods2()
    {
        $pub_id = CC('jtklm_pubId');
        $page = $this->request->param('page',1);
        $pageSize = $this->request->param('pageSize',20);
        $third_cids = $this->request->param('third_cids');
        $sort_type = $this->request->param('sort_type',0);
        $product_url = $this->request->param('product_url');
        if (!empty($product_url)){
            //获得转链口令
            $url = "http://api.act.jutuike.com/dyfx/product_privilege?pub_id={$pub_id}&product_url={$product_url}&sid=123456";
            $linshi_data = $this->paqu_jiekou($url);
            if (empty($linshi_data['data'])){
                $this->error('未找到该商品');
            }
            $common = $linshi_data['data']['dy_password'];
            $qinqiu = [
                'pub_id' => $pub_id,
                'command' => $common,
            ];
            $new_url = "http://api.act.jutuike.com/dyfx/shareCommandParse";
            $data_info = $this->paqu_jiekou_POST($new_url,$qinqiu);
            $data['data'][] = $data_info['data']['product_info'];
        }else{
            if (empty($third_cids)){
                $data_third_cids = $this->dd_goods2_cateid();
                $third_cids = $data_third_cids['category_id'];
            }
            $url = "http://api.act.jutuike.com/dyfx/product_search?pub_id={$pub_id}&page={$page}&pageSize={$pageSize}&third_cids={$third_cids}&sort_type={$sort_type}";
            $data = $this->paqu_jiekou($url);
        }
        if (!empty($data['data'])){
            $productId_list = [];
            foreach ($data['data'] as $data_item){
                $productId_list[] = $data_item['product_id'];
            }
            $new_data = $data['data'];
            $dd_goods_list = Db::name('ddss')
                ->field("goods_id")
                ->where('goods_id','in',$productId_list)
                ->select();
            $dd_goodsID_list = [];
            //判断是否已经插入过数据库
            if (!empty($dd_goods_list)){
                foreach ($dd_goods_list as $dd_goods_item){
                    $dd_goodsID_list[] = $dd_goods_item['goods_id'];
                }
            }
            $insert_goods = [];
            $data_goods = [];
            foreach ($new_data as $item){
                if (!in_array($item['product_id'],$dd_goodsID_list)){
                    $data_goods['goods_id'] = $item['product_id'];
                    $data_goods['image_url'] = $item['cover'];
                    $data_goods['title'] = $item['title'];
                    if (strpos($item['sales'],"万") !== false){
                        $sales = explode("万",$item['sales']);
                        $data_goods['sales_num'] = bcmul($sales[0],10000);
                    }else{
                        $data_goods['sales_num'] = $item['sales'];
                    }
                    $data_goods['price'] = $item['price'];
                    $data_goods['coupons_price'] = $item['coupon_price'];
//                $data_goods['evaluation'] = $item['comments'];
                    //24小时销量
                    $data_goods['sales24_num'] = 0;
                    //运费
                    $data_goods['freight'] = 0;
                    //好评率
                    $data_goods['positive_review_rate'] = 0;
                    //上架时间
//            $data_goods['listing_time'] = $item['parent_cat_id'];
                    //店铺名称
                    $data_goods['shop_name'] = $item['shop_name'];
                    //店铺商品数
//                $data_goods['shop_goods_num'] = $item['shopid'];
                    //店铺评分
//                $data_goods['shop_rating'] = $item['shopid'];
                    //分类
                    $data_goods['cate1_id'] = $item['first_cid'];
                    $data_goods['cate2_id'] = $item['second_cid'];
                    $data_goods['cate3_id'] = $item['third_cid'];
                    //商品链接
                    $data_goods['detail_url'] = $item['detail_url'];
                    $insert_goods[] = $data_goods;
                }

            }
            if (!empty($insert_goods)){
                Db::name('ddss')->insertAll($insert_goods);
            }
        }
        if (empty(!$product_url)){
            $data_info['data']['product_info']['product_id'] = (string)$data_info['data']['product_info']['product_id'];
            $this->success('successful',$data_info['data']['product_info']);
        }
        $newpage = $page+1;
        echo "三级分类为{$third_cids}的第{$page}页采集成功，正在跳转至下第{$newpage}页....";
        sleep(1);

        //下一个三级分类的开始
        if ($data['total_results'] > $page*$pageSize && !empty($data['data'])){
            $new_third_cids = $third_cids;

        }else{
            $data_goods_status = $this->dd_goods2_cateid();
            if (empty($data_goods_status)){
                Db::name('dd_category')->where('level',3)->update(['goods_status' => 0]);
                exit('采集完成');
            }
            $new_third_cids = $data_goods_status['category_id'];
            $newpage = 1;
            //修改这个分类状态
            Db::name('dd_category')->where('category_id',$third_cids)->update(['goods_status' => 1]);
        }
        //如果这个三级分类总数为2000，怎倒叙在来一遍
        if ($data['total_results'] == 2000 && !empty($this->lastpage($page,$pageSize,$data['total_results']))){
            if ($sort_type == 0 ){
                $new_third_cids = $third_cids;
                $sort_type = 1;
                $newpage = 1;
            }elseif ($sort_type == 1){
                $sort_type = 0;
                //修改这个分类状态
                Db::name('dd_category')->where('category_id',$third_cids)->update(['goods_status' => 1]);
                $data_goods_status = $this->dd_goods2_cateid();
                if (empty($data_goods_status)){
                    Db::name('dd_category')->where('level',3)->update(['goods_status' => 0]);
                    exit('采集完成');
                }
                $new_third_cids = $data_goods_status['category_id'];
                $newpage = 1;

            }
        }

        echo "<script>window.location.href='/api/products/dd_goods2?third_cids={$new_third_cids}&page={$newpage}&sort_type={$sort_type}';</script>";
        exit();
    }

    private function dd_goods2_cateid()
    {
        $dd_cate = Db::name('dd_category')
            ->field('id,category_id,name,level,parent_id,is_leaf,is_status,goods_status')
            ->where('level',3)
            ->where('goods_status',0)
            ->find();
        return $dd_cate;
    }

    //求取最后一页
    private function lastpage($page,$pageSize,$total_results){
        $lastpage = null;
        $newpage = $page+1;
        if ($newpage*$pageSize>=$total_results && $total_results != $page*$pageSize){
            $lastpage = $newpage;

        }elseif ($total_results <= $page*$pageSize){
            $lastpage = $page;
        }
        return $lastpage;
    }


    //方式二
    //分类
    public function dd_cate_caji()
    {
        $pub_id = CC('jtklm_pubId');
        $level = $this->request->param('level',1);
        $dd_cate = $this->dd_cate();
        if ($dd_cate['dd_cate_list'] == 0){
            $parent_id = 0;
        }else{
            $dd_cate_new = $this->dd_cate();
            if (empty($dd_cate_new['dd_cate'])){
                echo "采集完成...";
                exit();
            }
            //上级id
            $parent_id = $dd_cate_new['dd_cate']['category_id'];
            //要爬取的等级
            $level = $dd_cate_new['dd_cate']['level']+1;

        }
        $url = "http://api.act.jutuike.com/dyfx/product_category?pub_id={$pub_id}&parent_id={$parent_id}";
        $data = $this->paqu_jiekou($url);

        $dd_cate_list = Db::name('dd_category')
            ->field('id,category_id,name,level,parent_id,is_leaf,is_status')
            ->where('level',$level)
            ->select();
        $insert_cate = [];
        $data_cate = [];
        $new_cate_list = $data['data'];
        foreach ($new_cate_list as $item){
            if ($level == $item['level']){
                $data_cate['category_id'] = $item['id'];
                $data_cate['name'] = $item['name'];
                $data_cate['level'] = $item['level'];
                $data_cate['parent_id'] = $parent_id;
            }
            //判断是否已经插入过数据库
            if (!empty($dd_cate_list)){
                foreach ($dd_cate_list as $cate_item){
                    if ($cate_item['category_id'] == $item['id']){
                        unset($data_cate);
                    }
                }
            }
            if (!empty($data_cate)){
                $insert_cate[] = $data_cate;
            }
        }
        if (!empty($insert_cate)){
            Db::name('dd_category')->insertAll($insert_cate);
        }
        if ($parent_id != 0 && !empty($parent_id)){
            Db::name('dd_category')->where('category_id',$parent_id)->update(['is_status' => 1]);
        }

        echo "上级id为{$parent_id}采集成功，正在跳转至下第一条....";

        sleep(1);

        echo "<script>window.location.href='/api/products/dd_cate_caji?level={$level}';</script>";
        exit();
    }

    private function dd_cate($level = 1)
    {
        $dd_cate = Db::name('dd_category')
            ->field('id,category_id,name,level,parent_id,is_leaf,is_status')
            ->where('is_status',0)
            ->find();
        $dd_cate_list = Db::name('dd_category')
            ->field('id,category_id,name,level,parent_id,is_leaf,is_status')
            ->where('level',1)
            ->count();
        $result = [
            'dd_cate' => $dd_cate,
            'dd_cate_list' => $dd_cate_list,
        ];
        return $result;
    }



    //临时-每个拉去20个数据
    public function dd_goods3()
    {
        $pub_id = CC('jtklm_pubId');
        $page = $this->request->param('page',1);
        $pageSize = $this->request->param('pageSize',20);
        $third_cids = $this->request->param('third_cids');
        $sort_type = $this->request->param('sort_type',0);
        if (empty($third_cids)){
            $data_third_cids = $this->dd_goods2_cateid();
            $third_cids = $data_third_cids['category_id'];
        }
        $url = "http://api.act.jutuike.com/dyfx/product_search?pub_id={$pub_id}&page={$page}&pageSize={$pageSize}&third_cids={$third_cids}&sort_type={$sort_type}";
        $data = $this->paqu_jiekou($url);
        if (!empty($data['data'])){
            $productId_list = [];
            foreach ($data['data'] as $data_item){
                $productId_list[] = $data_item['product_id'];
            }
            $new_data = $data['data'];
            $dd_goods_list = Db::name('ddss')
                ->field("goods_id")
                ->where('goods_id','in',$productId_list)
                ->select();
            $dd_goodsID_list = [];
            //判断是否已经插入过数据库
            if (!empty($dd_goods_list)){
                foreach ($dd_goods_list as $dd_goods_item){
                    $dd_goodsID_list[] = $dd_goods_item['goods_id'];
                }
            }
            $insert_goods = [];
            $data_goods = [];
            foreach ($new_data as $item){
                if (!in_array($item['product_id'],$dd_goodsID_list)){
                    $data_goods['goods_id'] = $item['product_id'];
                    $data_goods['image_url'] = $item['cover'];
                    $data_goods['title'] = $item['title'];
                    $data_goods['sales_num'] = $item['sales'];
                    $data_goods['price'] = $item['price'];
                    $data_goods['coupons_price'] = $item['coupon_price'];
//                $data_goods['evaluation'] = $item['comments'];
                    //24小时销量
                    $data_goods['sales24_num'] = 0;
                    //运费
                    $data_goods['freight'] = 0;
                    //好评率
                    $data_goods['positive_review_rate'] = 0;
                    //上架时间
//            $data_goods['listing_time'] = $item['parent_cat_id'];
                    //店铺名称
                    $data_goods['shop_name'] = $item['shop_name'];
                    //店铺商品数
//                $data_goods['shop_goods_num'] = $item['shopid'];
                    //店铺评分
//                $data_goods['shop_rating'] = $item['shopid'];
                    //分类
                    $data_goods['cate1_id'] = $item['first_cid'];
                    $data_goods['cate2_id'] = $item['second_cid'];
                    $data_goods['cate3_id'] = $item['third_cid'];
                    //商品链接
                    $data_goods['detail_url'] = $item['detail_url'];
                    $insert_goods[] = $data_goods;
                }

            }
            if (!empty($insert_goods)){
                Db::name('ddss')->insertAll($insert_goods);
            }

        }

        echo "三级分类为{$third_cids}采集成功，正在跳转至下个三级分类....";
        //修改这个分类状态
        Db::name('dd_category')->where('category_id',$third_cids)->update(['goods_status' => 1]);
        sleep(1);

        $data_goods_status = $this->dd_goods2_cateid();
        if (empty($data_goods_status)){
            exit('采集完成');
        }
        $new_third_cids = $data_goods_status['category_id'];
        $newpage = 1;


        echo "<script>window.location.href='/api/products/dd_goods3?third_cids={$new_third_cids}&page={$newpage}&sort_type=0';</script>";
        exit();
    }


}